package com.pg.agent.engine.client.vo;

import com.pg.agent.engine.client.dto.AgentExeDTO;
import com.pg.agent.engine.client.dto.SOPCallVO;
import com.pg.agent.engine.client.model.SessionObject;
import com.pg.agent.engine.client.model.agent.AIAgent;
import com.pg.agent.engine.client.model.environment.AIEnvironment;
import com.pg.agent.engine.client.model.llm.CallLog;
import com.pg.agent.engine.client.model.sop.AISOP;
import com.pg.agent.engine.client.model.state.AIState;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 智能体会话执行对象
 *
 * @author huhuaping
 * @date 2023/11/20 10:25
 */
@Data
public class AIAgentSession {

    /**
     * 本次会话返回结果
     */
    private SOPCallVO sOPCallVO;
    /**
     * 当前会话上下文环境
     */
    private AIEnvironment environment;
    /**
     * 当前会话的SOP
     */
    private AISOP sop;
    /**
     * 当前会话执行的state
     */
    private AIState state;
    /**
     * 当前会话执行的agent
     */
    private AIAgent agent;
    /**
     * 会话ID sessionId
     */
    private String sessionId;
    /**
     * 执行ID
     */
    private Integer exeId;

    private String userKey;

    private String messageId;
    /**
     * 提问
     */
    private String query;
    /**
     * 是否调试
     */
    private boolean debug = false;
    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 本次会话执行，调用大模型日志
     */
    private List<CallLog> logList = new ArrayList<>();

    private Map<String, Object> userParam;

    public AIAgentSession(AIEnvironment environment, AISOP sop, String sessionId, Integer exeId) {
        this.environment = environment;
        this.sop = sop;
        this.sessionId = sessionId;
        this.exeId = exeId;
    }

    public AIAgentSession(AIEnvironment environment, SessionObject sessionObject, SOPCallVO sOPCallVO) {
        this.environment = environment;
        this.sop = sessionObject.getAiSOP();
        this.sessionId = sessionObject.getSessionId();
        this.exeId = sessionObject.getExeNum();
        this.userId = sessionObject.getUserId();
        this.sOPCallVO = sOPCallVO;
    }

    public AIAgentSession(AIEnvironment environment, SessionObject sessionObject, SOPCallVO sOPCallVO, Map<String,Object> userParam) {
        this.environment = environment;
        this.sop = sessionObject.getAiSOP();
        this.sessionId = sessionObject.getSessionId();
        this.exeId = sessionObject.getExeNum();
        this.userId = sessionObject.getUserId();
        this.sOPCallVO = sOPCallVO;
        this.userParam = userParam;
    }
    public AIAgentSession(AIEnvironment environment, SessionObject sanagerObject, SOPCallVO sOPCallVO, Boolean debug) {
        this.environment = environment;
        this.sop = sanagerObject.getAiSOP();
        this.sessionId = sanagerObject.getSessionId();
        this.exeId = sanagerObject.getExeNum();
        this.userId = sanagerObject.getUserId();
        this.sOPCallVO = sOPCallVO;
        this.debug = debug;
    }

    public AIAgentSession(AIEnvironment environment, SessionObject sanagerObject, SOPCallVO sOPCallVO, String userKey, Boolean debug, String query) {
        this.environment = environment;
        this.sop = sanagerObject.getAiSOP();
        this.sessionId = sanagerObject.getSessionId();
        this.exeId = sanagerObject.getExeNum();
        this.userId = sanagerObject.getUserId();
        this.sOPCallVO = sOPCallVO;
        this.userKey = userKey;
        this.debug = debug;
        this.query = query;
    }
    public AIAgentSession(AIEnvironment environment, SessionObject sanagerObject, SOPCallVO sOPCallVO, String userKey, AgentExeDTO runSopDTO) {
        this.environment = environment;
        this.sop = sanagerObject.getAiSOP();
        this.sessionId = sanagerObject.getSessionId();
        this.exeId = sanagerObject.getExeNum();
        this.userId = sanagerObject.getUserId();
        this.sOPCallVO = sOPCallVO;
        this.userKey = userKey;
        this.debug = runSopDTO.isDebug();
        this.query = runSopDTO.getContent();
        this.userParam = runSopDTO.getUserParam();
        this.messageId = runSopDTO.getDataId();
    }

    public AIAgentSession() {
    }

    /**
     * 获取本次对话记录ID
     *
     * @return
     */
    public String getDataId() {
        return sessionId + "-" + exeId;
    }

    public void addCallLog(CallLog callLog, String callResponse){

        callLog.setCallEndTime(new Date());
        callLog.setTimeLong(System.currentTimeMillis() - callLog.getCallStartTime().getTime());
        callLog.setResponse(callResponse);

        logList.add(callLog);
    };
}
